use sqlx::{
    mysql::{MySql, MySqlConnectOptions, MySqlPoolOptions},
    Pool,
};

#[allow(unused)]
const DATABASE_URL: &str = "mysql://dapp:4SCFrHj67XmM6WCF@45.154.3.19:3385/dapp";

pub async fn pool(env: &str) -> anyhow::Result<Pool<MySql>> {
    let opts = build_opts(env);

    let pool = MySqlPoolOptions::new()
        .min_connections(10)
        .max_connections(10)
        .test_before_acquire(false)
        .connect_with(opts)
        .await?;

    Ok(pool)
}

fn build_opts(env: &str) -> MySqlConnectOptions {
    let dev_opts = MySqlConnectOptions::new()
        .host("localhost")
        .port(3306)
        .username("totem")
        .password("totem123")
        .database("mall");

    let test_opts = MySqlConnectOptions::new()
        .host("localhost")
        .port(3306)
        .username("root")
        .password("Pqt38VMqV4aV2HK")
        .database("dapp");

    let prod_opts = MySqlConnectOptions::new()
        .host("localhost")
        .port(3306)
        .username("root")
        .password("Pqt38VMqV4aV2HK")
        .database("dapp");

    match env {
        "TEST" => dev_opts,
        "PROD" => prod_opts,
        _ => prod_opts,
    }
}
